var app = getApp();
Page({

  data: {
    info:[],
    itemList: ['增加', '减少'],
    status:'点击选择状态',
    choose:'',
    userInfo: null,
    title:'',
    zhicheng:'',
    lid:'9999999',
    
  },

  onLoad: function (options) {
    
    this.setData({ lid: options.scene });
  
    this.getlist(options.scene)
    // this.setData({ lid: 50});
     //this.getlist(50)

  },

  onShow: function () {
    if (!wx.getStorageSync('userInfo')) {
      app.error("请登录后再操作", function () {
        setTimeout(function () { wx.navigateBack({}) }, 2000);
      });
    }else{

      this.setData({ userInfo:wx.getStorageSync('userInfo')});

      this.setData({ title:wx.getStorageSync('title')});

      if(wx.getStorageSync('gerenziliao')){

        this.setData({ userinfo:wx.getStorageSync('gerenziliao')});
      }
    

  
    }
  },

  changezt(){
    var that = this
    wx.showActionSheet({
      itemList: ['增加', '减少'],
      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex});
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  formSubmit: function (event) {
    var that = this;
    console.log(that.data.choose)


    if(that.data.choose === ""){
      app.error('请选择状态');
      return;
    }
    if (event.detail.value.kucun > 0) {
     
    }else{
      app.error('请输入库存数量');
      return;
    }

      console.log(event.detail.value.kucun )
    wx.request({
      url: app.apiUrl + '/api/data/CStore',
      data: {
        choose:that.data.choose,
        stock: event.detail.value.kucun,
        openid:wx.getStorageSync('openid'),
        id:this.data.lid
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
    
       if(res.data.code == 1){

        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
        
        setTimeout(function () {
          wx.navigateTo({
            url: '/page/my/index',
          });
        }, 2000)
       }else{
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          showCancel: false
        });
       }

      }
    })

  },

  getlist:function (lid){
    // var lid = 1

    var that = this
    wx.request({
      url: app.apiUrl + '/api/index/getstore',
      data: {
        openid:wx.getStorageSync('openid'),
        groupid:wx.getStorageSync('groupid'),
        id:lid,
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success: function (res) {
  
     
        if(res.data.code == 1){
          that.setData({ info: res.data.data});
              
        }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
            });
            setTimeout(function () {
              wx.navigateTo({
                url: '/page/my/index',
              });
            }, 2000)

        }
     
      }
    });
  },
  //上传头像
  uploadAvatar: function () {
    var that = this;
    wx.chooseImage({
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var formData = app.globalData.config.upload.multipart;
        formData.token = app.globalData.userInfo.token;
        wx.uploadFile({
          url: app.globalData.config.upload.uploadurl,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: formData,
          success: function (res) {
            var row = JSON.parse(res.data);
            if (row.code == 1) {
              app.success('头像上传成功');              
              that.setData({ ["userInfo.avatar"]: app.globalData.config.upload.cdnurl + row.data.url });
            }
          },
          error: function (e) {
            console.log(e);
          }
        });
      }, error: function () {
        console.log(e);
      }
    });
  }

})